<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-13
 ***********************************************************************/
if(defined("IVANENGINE_CORE_EXCEPTION"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_EXCEPTION", true);

class CException extends Exception 
{
	protected $m_message	= "";
	
	public function __construct($message) 
	{
		$numargs 	= func_num_args();
		if ($numargs >= 2) {
			$arg_list 	= func_get_args();
			$temp_args	= array();
			for ($i = 1; $i < $numargs; $i++) {
				$temp_args[$i-1] = $arg_list[$i];
    		}
    		$this->m_message = vsprintf($message, $temp_args);
		}else{
			$this->m_message = $message;
		}
		parent::__construct($this->m_message);
	}
	
	function GetStackList()
	{
		$result = array();
		$list 	= split("#", $this->getTraceAsString());
		for ($i = 0; $i < count($list); $i++ ){
			$result[$i] = substr($list[$i], 2, strlen($list[$i])-2);
		}
		return	$result;
	}
	
	function GetFullMessage()
	{
		$result = "";
		$array  = $this->GetStackList();
		for ($i = 1; $i < count($array); $i++ ){
			$result .= "[".$i."] ".$array[$i]."<br>";
		}
        $result   = str_replace('\\', '/', $result);
		return "Exception message: <br>".$this->m_message."<br>Stack trace:<br>".$result;
	}
	
	function PushToLog()
	{
		CLog::Get()->Fatal($this->GetFullMessage());
	}
}

?>